/*-----------------------------------------------------
 * 权限子系统-系统组织机构模块
 * ---------------------------------------------------*/
jx.ready(function () {
    window.api = {
        grid: '/sys/dept/grid',
        create: '/sys/dept/create',
        edit: '/sys/dept/edit',
        delete: '/sys/dept/delete',
        details: '/sys/dept/details',
        export: '/sys/dept/export',
        status: '/sys/dept/status',
        // authorize: '/auth/objectmap/setting',
        dnd: '/sys/dept/dnd'
    };
    //定义变量
    var $grid = $('#grid'), $gridPanel, gridInstance;
    var dialogWidth = $grid.data('dialogWidth');
    var dialogHeight = $grid.data('dialogHeight');
    var gridUrl = jx.apiUrl(api.grid);
    var createUrl = jx.apiUrl(api.create);
    var editUrl = jx.apiUrl(api.edit);
    var deleteUrl = jx.apiUrl(api.delete);
    var detailsUrl = jx.apiUrl(api.details);
    var exportUrl = jx.apiUrl(api.export);
    var statusUrl = jx.apiUrl(api.status);
    var dndUrl = jx.apiUrl(api.dnd);
    // var authorizeUrl = jx.apiUrl(api.authorize);

    //初始化表格
    var initGrid = function () {
        gridInstance = $grid.jxtreegrid({
            url: gridUrl,
            dndUrl: dndUrl,
            onDblClickRow: function (row) {
                var id = gridInstance.getRowId(row);
                jx.auth.showDepartmentDetails(id);
            },
            onLoadSuccess: function () {
                gridInstance.getDataBodyPanel().contextmenu({target: '#gridcmenu'});
            },
            onContextMenu: function (e, row) {
                if (row) {
                    var id = gridInstance.getRowId(row);
                    gridInstance.select(id);
                }
            },
            loadFilter:function (data) {
                data && data.rows && data.rows.forEach(function (v) {
                    if (v._parentId) {
                        if(!hasParentNode(data.rows,v.parentId)){
                            v._parentId = null;
                        }
                    }
                });
                return data;
            }
        });
        $gridPanel = gridInstance.getPanel();
    };

    /**
     * 是否存在父节点
     * @param rows
     * @param parentId 父节点 Id
     * @returns {boolean}
     */
    var hasParentNode = function (rows, parentId) {
        for (var i = 0; i < rows.length; i++) {
            if (rows[i].id == parentId) return true;
        }
        return false;
    };

    //初始化事件
    var initEvent = function () {
        $('#btn-createroot,#btn-cmcreateroot').click(function () {
            createRootData();
        });
        $('#btn-create,#btn-cmcreate').click(function () {
            createData();
        });
        $('#btn-edit,#btn-cmedit').click(function () {
            editData();
        });
        $('#btn-delete,#btn-cmdelete').click(function () {
            deleteData();
        });
        $('#btn-export').click(function () {
            exportData();
        });
        // $('#btn-authorize').click(function () {
        //     authorize();
        // });

        // 表格事件
        $gridPanel.on('click', '.gridstatus', function () {
            var id = $(this).data('id');
            var val = $(this).data('val');
            setStatus(id, val);
        });
    };

    //新增根节点数据
    var createRootData = function () {
        jx.dialog({
            title: '新增根组织机构',
            url: createUrl,
            width: dialogWidth,
            height: dialogHeight
        });
    };

    //新增数据
    var createData = function () {
        var parentId = gridInstance.getSelectedRowId() || 0;
        jx.dialog({
            title: '新增组织机构',
            url: createUrl,
            params: {parentId: parentId},
            width: dialogWidth,
            height: dialogHeight
        });
    };

    //编辑数据
    var editData = function () {
        if (!gridInstance.hasSelectedRow()) return;
        var id = gridInstance.getSelectedRowId();
        jx.dialog({
            title: '修改组织机构',
            url: editUrl,
            params: {id: id},
            width: dialogWidth,
            height: dialogHeight
        });
    };

    //删除数据
    var deleteData = function () {
        if (!gridInstance.hasSelectedRow()) return;
        var id = gridInstance.getSelectedRowId();
        jx.delete({
            confirm: '注：如果有子节点则都会被删除，并且无法撤销，您确定要删除吗？',
            url: deleteUrl,
            data: {id: id},
            success: function (result) {
                gridInstance.reloadGridData();
            }
        });
    };

    //导出数据
    var exportData = function () {
        jx.auth.exportData(exportUrl, jx.serialize($('#gridform')));
    };

    //设置数据状态
    var setStatus = function (id, val) {
        var status = val == '1' ? 0 : 1;
        jx.ajax({
            url: statusUrl,
            data: {id: id, status: status},
            maskMsg: '正在更新状态,请稍等...',
            success: function (result) {
                gridInstance.reloadGridData();
                jx.toastr.success('状态更新成功');
            }
        });
    };

    // //设置权限
    // var authorize = function () {
    //     if (!gridInstance.hasSelectedRow()) return;
    //     var ids = gridInstance.getCheckedRowIds();
    //     if (ids.length > 1) {
    //         jx.toastr.error('只能选择一项进行操作！');
    //         return;
    //     }
    //     var id = gridInstance.getSelectedRowId();
    //     var row = gridInstance.getSelected();
    //     jx.dialog({
    //         title: '部门授权 - ' + row.name,
    //         url: authorizeUrl,
    //         params: {objectName: 'Department', objectId: id},
    //         width: '700px',
    //         height: '80%'
    //     });
    // }

    //定义表格列格式化函数

    //对外接口
    //重新加载表格数据
    window.reloadGridData = function () {
        gridInstance.reloadGridData();
    };

    //初始化
    initGrid();
    initEvent();
});